package com.game.modular.game.act.playerInfo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.game.comment.core.ResponseResult;
import com.game.comment.util.JwtUtils;

import com.game.modular.game.act.playerInfo.vo.PlayerInfoVo;
import com.game.modular.game.playerInfo.domain.PlayerRank;
import com.game.modular.game.playerInfo.service.PlayerRankServiceImpl;
// import com.game.modular.game.playerInfo.service.PlayerServiceImpl;
// import com.game.modular.game.playerInfo.service.PlayerServiceImpl;
import com.game.modular.game.playerInfo.service.PlayerServiceImpl;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 游戏账户(Player)表服务控制层
 *
 * @author cxy
 * @since 2024-07-18 10:05:14
 */
@Api(tags = "账号行为")
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("/playerAct")
public class PlayerAction {
    @Resource
    private PlayerServiceImpl playerService;
    @Resource
    private PlayerRankServiceImpl playerRankService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public ResponseResult<?> login(@RequestHeader("token") String token) {
        Long playerId = JwtUtils.getPlayerId(token);
        String serveKey = JwtUtils.getServeKey(token);
        return ResponseResult.success("登录成功", playerService.login(playerId, serveKey));
    }

    @ApiOperation(value = "更新用户的信息")
    @PostMapping("/createUser")
    public ResponseResult<String> createUser(
            @RequestHeader("token") String token,
            @Valid @RequestBody PlayerInfoVo.PlayerVo.CreateUser param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        return ResponseResult.success("创建用户成功", playerService.createUser(
                playerId, param.getNickname(), param.getSex(), param.getAvatar(), param.getCamp()
        ));
    }

    @ApiOperation(value = "获取用户信息")
    @PostMapping("/getUserInfo")
    public ResponseResult<?> select(@RequestHeader String token) {
        return ResponseResult.success(playerService.select(JwtUtils.getPlayerId(token)));
    }

    @ApiOperation(value = "胜点排行")
    @PostMapping("/rankingOfWinningPoints")
    public ResponseResult<Set<PlayerRank>> rankingOfWinningPoints(
            @RequestHeader String token
    ) {
        LambdaQueryWrapper<PlayerRank> queryWrapper = new LambdaQueryWrapper<PlayerRank>()
                .orderByAsc(PlayerRank::getRankIntegral)
                .last("limit 100");
        List<PlayerRank> list = playerRankService.list(queryWrapper);
        Long playerId = JwtUtils.getPlayerId(token);
        Set<PlayerRank> playerRankList = new HashSet<>(list);
        PlayerRank one = playerRankService.getOne(new LambdaQueryWrapper<PlayerRank>().eq(PlayerRank::getUid, playerId));
        playerRankList.add(one);
        return ResponseResult.success(playerRankList);
    }
    @ApiOperation(value = "段位排名")
    @PostMapping("/rankRanking")
    public ResponseResult<Set<PlayerRank>> rankRanking(
            @RequestHeader String token
    ) {
        LambdaQueryWrapper<PlayerRank> queryWrapper = new LambdaQueryWrapper<PlayerRank>()
                .orderByAsc(PlayerRank::getRank)
                .last("limit 100");
        List<PlayerRank> list = playerRankService.list(queryWrapper);
        Long playerId = JwtUtils.getPlayerId(token);
        Set<PlayerRank> playerRankList = new HashSet<>(list);
        PlayerRank one = playerRankService.getOne(new LambdaQueryWrapper<PlayerRank>().eq(PlayerRank::getUid, playerId));
        playerRankList.add(one);
        return ResponseResult.success(playerRankList);
    }
    @ApiOperation(value = "主公等级排行")
    @PostMapping("/rankRankingOfPrincipalOfficials")
    public ResponseResult<Set<PlayerRank>> rankRankingOfPrincipalOfficials(
            @RequestHeader String token
    ) {
        LambdaQueryWrapper<PlayerRank> queryWrapper = new LambdaQueryWrapper<PlayerRank>()
                .orderByAsc(PlayerRank::getGrade)
                .last("limit 100");
        List<PlayerRank> list = playerRankService.list(queryWrapper);
        Long playerId = JwtUtils.getPlayerId(token);
        Set<PlayerRank> playerRankList = new HashSet<>(list);
        PlayerRank one = playerRankService.getOne(new LambdaQueryWrapper<PlayerRank>().eq(PlayerRank::getUid, playerId));
        playerRankList.add(one);
        return ResponseResult.success(playerRankList);
    }

    @ApiOperation(value = "获取用户信息")
    @PostMapping("/select")
    public ResponseResult<?> EqSelect(
            @RequestBody PlayerInfoVo.PlayerVo.EqSelect param) {
        return ResponseResult.success(
               playerService.select(param.getUid())
        );
    }
    public ResponseResult<List<String>> select(){
        List<String> manMachine = playerService.getManMachine();
        return ResponseResult.success(manMachine);
    }
}
